package hoaitt.vnu.edu.problem4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FindPlindromeMax {
	
	/**
	 * Check a number is a plindrome?
	 * @param number
	 * @return
	 */
	public boolean checkPlindrome(int number) {
		boolean tmp = false;
		String temp = Integer.toString(number);
		String reverse = new StringBuffer(temp).reverse().toString();
		// compare
		if(temp.equals(reverse)) { 
			tmp = true;
		}
		return tmp;
	}
	
	public int[] findMaxNumber() {
		int[] result = {0,0,0};
		int tmp = 0;
		
		// Cái bước lặp này là lùi đúng ko ? từ lớn trở về.
		for (int i = 999; i >=100; i--) {
			for (int j = i; j >= 100; j--) {
				tmp = i * j;
				if (checkPlindrome(tmp)){
					result[0] = tmp;
					result[1] = i;
					result[2] = j;
					return result;
				}
			}
			
		}
		return result; 
	}
	
	public static void main(String[] args) {
		FindPlindromeMax test = new FindPlindromeMax();
		System.out.println(test.findMaxNumber()[2] + " "); // 995 583
		
		List<Integer> temp = new ArrayList<Integer>();
		
		for (int i = 995; i >= 585; i--) {
			for (int j = 585; j < i; j++) { // giao hoan phep nhan
				int tp = i * j;
				if (test.checkPlindrome(tp)) {
					temp.add(tp);
				}
			}
		}
		
		Collections.sort(temp);
		System.out.println(temp);
	}

}
